import { Component } from 'react'
import Taro from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './index.scss'
import global from '../../global';
import { Textarea, Button, Map, Swiper, SwiperItem, Picker, Input, Image } from '@tarojs/components'
import Request from "../../api";
import Event from "../../event";
export default class Index extends Component {

  constructor(props) {
    super(props);
    this.state = {
      houses: []
    };
    this.loadHouses = this.loadHouses.bind(this);
    this.deleteJob = this.deleteJob.bind(this);
    this.updateStatus = this.updateStatus.bind(this);
    Event.on('updateHouses', this.loadHouses)
  }

  componentWillMount () {
    this.loadHouses();
  }

  componentWillUnmount () { }

  componentDidHide () { }

  async loadHouses() {
    try {
      let res = await Request.get('/business/rental_house/list?userId=' + global.userInfo.userId);
      if (res.data.code !== 200) {
        throw new Error();
      }
      this.setState({
        houses: res.data.rows
      })
    } catch (e) {
      console.log('加载职位数据失败')
    }
  }

  async deleteJob(item) {
    await Taro.showLoading({
      title: '刪除中...'
    });
    try {
      let res = await Request.delete('/business/rental_house/' + item.rentalHouseId);
      if (res.data.code !== 200) {
        throw new Error();
      }
      await Taro.hideLoading();
      await Taro.showToast({
        title: '移除成功',
        type: 'success'
      })
      await this.loadHouses();
    } catch (e) {
      await Taro.hideLoading();
      await Taro.showToast({
        title: '移除失败请重试',
      })
    } finally {
      Taro.hideLoading();
    }
  }

  async updateStatus(item, status) {
    await Taro.showLoading({
      title: '更新状态中...'
    });
    item.status = status;
    try {
      let res = await Request.put('/business/rental_house', item);
      if (res.data.code !== 200) {
        throw new Error();
      }
      await Taro.hideLoading();
      await Taro.showToast({
        title: '更新成功',
        type: 'success'
      })
      await this.loadHouses();
    } catch (e) {
      await Taro.hideLoading();
      await Taro.showToast({
        title: '更新失败请重试',
      })
    } finally {
      Taro.hideLoading();
    }
  }

  render () {

    let  {houses} = this.state;
    return (
      <View className='job_list'>
        {
          houses.length !== 0 ? houses.map(item => {
            return (
              <View className='job_item'>

                <View className='row'>
                  <Text className='job_name'>{item.title}</Text>
                  <Text className='job_salary'>{item.rent}</Text>
                </View>
                <View className='row'>
                  <Text className='job_experience'>{item.model} {item.area} {item.direction} {item.level}  </Text>
                  <Text className='job_status' style={{color: item.status === '1' ? 'green' : 'red'}}>{item.status === '1' ? '招租中' : '停止招租'}</Text>
                </View>
                <View className='row'>
                  <View className='master_info'>
                    <Text>{item.village} {item.address}</Text>
                  </View>
                  <View className='action_container'>
                    {
                      item.status === '1' ? <Text className='size32 color-red' onClick={async () => {
                          this.updateStatus(item, '0')
                      }}>停止招租</Text>
                        :
                      <Text className='size32 color-green' onClick={async () => {
                        this.updateStatus(item, '1')
                      }}>开始招租</Text>
                    }
                    <Text className='size32 color-red' onClick={async () => {
                      await Taro.showModal({
                        title: '提示',
                        content: '确定删除该房源么？',
                        success: () => this.deleteJob(item)
                      })
                    }}>删除</Text>
                  </View>
                </View>

              </View>
            );
          })
            :
            <View className='message_container'>
              暂未发布相关房源，<Text onClick={() => Taro.navigateTo({url: '/pages/rental_house_detail/index'})}>点击发布房源</Text>
            </View>
        }
        {
          houses.length !== 0 && <View className='job_item add_btn' onClick={() => Taro.navigateTo({url: '/pages/rental_house_detail/index'})}>
            <Text className='iconfont icon-add size50 color-grey text_align_center'/>
          </View>
        }
      </View>
    )
  }
}
